#include "widget.h"
#include "ui_widget.h"
#include "qwy_calculate.h"
#include <QDebug>
#include <QString>

qwy_calculate qwy;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btn_growth_clicked()
{
    bool ok_line_growth_lastYear;
    bool ok_line_growth_nowYear;
    double line_growth;

    double line_growth_lastYear = ui->line_growth_lastYear->text().toDouble(&ok_line_growth_lastYear);
    double line_growth_nowYear = ui->line_growth_nowYear->text().toDouble(&ok_line_growth_nowYear);

    if(ok_line_growth_lastYear && ok_line_growth_nowYear){
        line_growth = qwy.qwy_growth(line_growth_lastYear,line_growth_nowYear);
        if(line_growth == 0)
            goto _exit_fun;
        QString str = QString::number(line_growth);
        ui->textBro_growth->setText(str+"%");
        return ;
    }

_exit_fun:
    ui->textBro_growth->setText("error");
    return;
}


void Widget::on_btn_proportion_clicked()
{
    bool ok_line_proportion_lastYear;
    bool ok_line_proportion_nowYear;
    double line_proportion;

    double line_proportion_lastYear = ui->line_proportion_lastYear->text().toDouble(&ok_line_proportion_lastYear);
    double line_proportion_nowYear = ui->line_proportion_nowYear->text().toDouble(&ok_line_proportion_nowYear);

    if(ok_line_proportion_lastYear && ok_line_proportion_nowYear){
        line_proportion = qwy.qwy_proportion(line_proportion_lastYear,line_proportion_nowYear);
        if(line_proportion == 0)
            goto _exit_fun;
        QString str = QString::number(line_proportion);
        ui->textBro_proportion->setText(str+"%");
        return ;
    }

_exit_fun:
    ui->textBro_growth->setText("error");
    return ;

}


void Widget::on_btn_total_num_clicked()
{

    double text_lastYear_Bro_total;
    double text_nowYear_Bro_total;
    QString str_last;
    QString str_now;

    QString lastYear = ui->textEdit_lastYear_num->toPlainText();
    QString nowYear = ui->textEdit_nowYear_num->toPlainText();

    qDebug() << "去年：" << lastYear;
    qDebug() << "今年：" << nowYear;


    text_lastYear_Bro_total = qwy.qwy_totla_add(lastYear);
    if(text_lastYear_Bro_total == 0){
        goto _exit_ui;
    }
    str_last = QString::number(text_lastYear_Bro_total);
    ui->textBro_total_lastYear->setText(str_last);

_exit_ui:
    text_nowYear_Bro_total = qwy.qwy_totla_add(nowYear);
    if(text_lastYear_Bro_total == 0){
        goto __exit_fun;
    }
    str_now = QString::number(text_nowYear_Bro_total);
    ui->textBro_total_nowYear->setText(str_now);


__exit_fun:
    return ;
}


void Widget::on_btn_total_growth_clicked()
{
    double text_lastYear_Bro_total;
    double text_nowYear_Bro_total;
    QString str_last;
    QString str_now;
    QString ret;

    QString lastYear = ui->textEdit_lastYear_num->toPlainText();
    QString nowYear = ui->textEdit_nowYear_num->toPlainText();

    qDebug() << "去年：" << lastYear;
    qDebug() << "今年：" << nowYear;


    text_lastYear_Bro_total = qwy.qwy_totla_add(lastYear);
    if(text_lastYear_Bro_total == 0){
        goto _exit_ui;
    }
    str_last = QString::number(text_lastYear_Bro_total);
    ui->textBro_total_lastYear->setText(str_last);

_exit_ui:
    text_nowYear_Bro_total = qwy.qwy_totla_add(nowYear);
    if(text_lastYear_Bro_total == 0){
        goto __exit_fun;
    }
    str_now = QString::number(text_nowYear_Bro_total);
    ui->textBro_total_nowYear->setText(str_now);

    ret = QString::number(qwy.qwy_growth(text_lastYear_Bro_total,text_nowYear_Bro_total));

    ui->textBro_total_growth->setText(ret+"%");


__exit_fun:
    return ;
}


void Widget::on_btn_total_proportion_clicked()
{
    double text_lastYear_Bro_total;
    double text_nowYear_Bro_total;
    QString str_last;
    QString str_now;
    QString ret;

    QString lastYear = ui->textEdit_lastYear_num->toPlainText();
    QString nowYear = ui->textEdit_nowYear_num->toPlainText();

    qDebug() << "去年：" << lastYear;
    qDebug() << "今年：" << nowYear;


    text_lastYear_Bro_total = qwy.qwy_totla_add(lastYear);
    if(text_lastYear_Bro_total == 0){
        goto _exit_ui;
    }
    str_last = QString::number(text_lastYear_Bro_total);
    ui->textBro_total_lastYear->setText(str_last);

_exit_ui:
    text_nowYear_Bro_total = qwy.qwy_totla_add(nowYear);
    if(text_lastYear_Bro_total == 0){
        goto __exit_fun;
    }
    str_now = QString::number(text_nowYear_Bro_total);
    ui->textBro_total_nowYear->setText(str_now);

    ret = QString::number(qwy.qwy_proportion(text_lastYear_Bro_total,text_nowYear_Bro_total));

    ui->textBro_total_proportion->setText(ret+"%");


__exit_fun:
    return ;
}

